package services

import (
	"regexp"
	"strings"
	"wecom_rebot/models"
)

// RemoveTitle 移除标题行
func RemoveTitle(msg *models.Msg) string {
	// 正则表达式匹配以"功能"开头的行（支持中英文冒号）
	pattern := regexp.MustCompile(`(?m)^功能\s*[:：].*$`)
	content := pattern.ReplaceAllString(msg.Content, "")

	// 移除空行
	emptyLinePattern := regexp.MustCompile(`(?m)^\s*$[\r?\n|\r|$]`)
	content = emptyLinePattern.ReplaceAllString(content, "")

	// 日志记录（统一换行符）
	cleanedContent := strings.NewReplacer("\r\n", "\n", "\r", "\n").Replace(content)
	copyAccountTxLogger.Info("过滤后 content 内容：\n" + cleanedContent)

	return content
}

// Contains 检查字符串是否在数组中
func Contains(arr []string, str string) bool {
	for _, a := range arr {
		if a == str {
			return true
		}
	}
	return false
}

// UniqueStrings 去重字符串切片
func UniqueStrings(slice []string) []string {
	seen := make(map[string]bool)
	var result []string

	for _, v := range slice {
		if !seen[v] {
			seen[v] = true
			result = append(result, v)
		}
	}

	return result
}

// 去重函数
func unique(intSlice []int64) []int64 {
	keys := make(map[int64]bool)
	var list []int64
	for _, entry := range intSlice {
		if _, value := keys[entry]; !value {
			keys[entry] = true
			list = append(list, entry)
		}
	}
	return list
}
